package com.zjc.springboot.zk.slice.server;

import org.apache.curator.test.TestingServer;

import java.io.File;
import java.io.IOException;


public final class ZookeeperServer {

    private static TestingServer testingServer;

    public static void start(int port) {
        try {
            testingServer = new TestingServer(port);

        } catch (final Exception ex) {
            ex.printStackTrace();
        } finally {
            Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                try {
                    Thread.sleep(1000L);
                    testingServer.close();
                } catch (final InterruptedException | IOException ignore) {
                }
            }));
        }
    }

    public static void start(int port, String path) {
        try {
            testingServer = new TestingServer(port, new File(path));

        } catch (final Exception ex) {
            ex.printStackTrace();
        } finally {
            Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                try {
                    Thread.sleep(1000L);
                    testingServer.close();
                } catch (final InterruptedException | IOException ignore) {
                }
            }));
        }
    }

    public static void main(String[] args) {
        ZookeeperServer.start(2181);
    }
}
